import Utils from "../../utils/Utils";

const websocket = require(`websocket`);
const {ccclass, property} = cc._decorator;

let STYPE_TALKROOM = 1;
let TalkCmd = {
    Enter: 1, //用户进来
    Exit: 2, //用户离开
    UserArrived: 3, //别人进来
    UserExit: 4, //别人离开
    SendMsg: 5, //自己发送消息
    UserMsg: 6  //别人发送消息
}

let Response = {
    OK: 1,
    IS_IN_TALKROOM: -100,       //玩家已经在聊天室
    NOT_IN_TALKROOM: -101,      //玩家不在聊天室
    INVALID_OPT: -102,          //玩家非法操作
    INVALID_PARAMS: -103,       //玩家参数不正确
}

@ccclass
export default class TalkRoom extends cc.Component {

    @property(cc.ScrollView)
    scrollView: cc.ScrollView = null;

    @property(cc.Prefab)
    tipsPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    talkSelfPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    talkOtherPrefab: cc.Prefab = null;

    @property(cc.EditBox)
    editBox: cc.EditBox = null;

    nickName: string = ``;
    sex: number = 1;

    onLoad(){
        websocket.register_services_handler({
            1: this.onTalkRoomServiceRecv.bind(this),
        });
        this.nickName = `游客` + Utils.randomIntStr(4);
        this.sex = Utils.randomInt(1, 2);
        this.connectToTalkRoom();
    }

    onTalkRoomServiceRecv(stype, cmd, body){
        switch(cmd){
            case TalkCmd.Enter:{
                console.log(`enter: `, body);
                cc.log(body == Response.OK);
                if(body == Response.OK){
                    this.showTips(`欢迎进入聊天室`);
                }
                break;
            }
            case TalkCmd.Exit:{
                console.log(`exit: `, body);
                if(body == Response.OK){
                    this.showTips(`你已离开聊天室`);
                }
                break;
            }
            case TalkCmd.UserArrived:{
                console.log(`userArraived: `, body);
                this.showTips(`${body.uname} 进入聊天室`);
                break;
            }
            case TalkCmd.UserExit:{
                console.log(`userExit: `, body);
                this.showTips(`${body.uname} 离开聊天室`);
                break;
            }
            case TalkCmd.SendMsg:{
                console.log(`sendMsg: `, body);
                if(body[0] == Response.OK){
                    this.addSelfTalk(body[1], body[3]);
                }
                break;
            }
            case TalkCmd.UserMsg:{
                console.log(`userMsg: `, body);
                this.addOtherTalk(body[0], body[2]);
                break;
            }
        }
    }
    
    showTips(text: string){
        let node = cc.instantiate(this.tipsPrefab);
        let lbl = node.getChildByName(`lblText`).getComponent(cc.Label);
        lbl.string = text;

        this.scrollView.content.addChild(node);
        this.scrollView.scrollToBottom(0.1);
    }

    addSelfTalk(name: string, msg: string){
        let node = cc.instantiate(this.talkSelfPrefab);
        let lblNickName = node.getChildByName(`lblNickName`).getComponent(cc.Label);
        lblNickName.string = name;
        let lblMsg = node.getChildByName(`lblMsg`).getComponent(cc.Label);
        lblMsg.string = msg;

        this.scrollView.content.addChild(node);
        this.scrollView.scrollToBottom(0.1);
    }

    addOtherTalk(name: string, msg: string){
        let node = cc.instantiate(this.talkOtherPrefab);
        let lblNickName = node.getChildByName(`lblNickName`).getComponent(cc.Label);
        lblNickName.string = name;
        let lblMsg = node.getChildByName(`lblMsg`).getComponent(cc.Label);
        lblMsg.string = msg;

        this.scrollView.content.addChild(node);
        this.scrollView.scrollToBottom(0.1);
    }

    start(){

    }

    connectToTalkRoom(){
        cc.log(`connectToTalkRoom`);
        websocket.send_cmd(STYPE_TALKROOM, TalkCmd.Enter, {
            uname: this.nickName,
            usex: this.sex 
        });
    }

    disconnectFromTalkRoom(){
        cc.log(`disconnectFromTalkRoom`);
        websocket.send_cmd(STYPE_TALKROOM, TalkCmd.Exit, null);
    }

    onBtnSend(){
        let str = this.editBox.string;
        if(!str || str.length <= 0) return;
        this.editBox.string = ``;
        websocket.send_cmd(STYPE_TALKROOM, TalkCmd.SendMsg, str);
    }
}
